<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 *
 */
 
namespace osc\mobile\controller;
use think\Db;
use think\Session;
use think\Request;
class Agent extends MobileBase
{
	protected function _initialize(){
		parent::_initialize();						
		define('UID',is_login());	
		//手机版
		if(!UID){		
			$this->redirect('login/login');							
		}
		if (!Session::has("username")) {

            $this->redirect('login/login');

        }
        $username = session('username');
		$agent = Db::name('agent')->where(array('username'=>$username))->find();	
		//if(!$agent)
		//{
			//$this->redirect('user/index');
		//}
		//var_dump($agent);
		if($agent['state']==2)
		{
			$this->redirect('user/index');
		}

	}

    function index()
    {
    	//$uid=$this->check_login();
		$username = session('username');

		$start = input('param.start');
		$end = input('param.end');
		if($start && $end){
			$map = [
					'username' => $username,
					'creat_time' => array(['>',strtotime($start)],['<',strtotime($end)])
			];
			$list = Db::name('agent_account')->where($map)->order('creat_time desc')->paginate(10,false, [
					'query' => Request::instance()->param(),//不丢失已存在的url参数
			]);
		}else{

			$list = Db::name('agent_account')
					->where(array('username'=>$username))
					->order('creat_time desc')
					->paginate(10);
		}
		$page=$list->render(); //获取分页

		$user_info = Db::name('member')->where(array('username'=>$username))->find();

		$this->assign('user_info',$user_info);
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('start',$start);
		$this->assign('end',$end);
		$this->assign('empty',"<span style='margin-left:20px;'>没有数据</span>");
				
		$this->assign('top_title','我的服务中心');
		$this->assign('SEO',['title'=>'我的服务中心-'.config('SITE_TITLE')]);
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');		
		return $this->fetch();
    }



	//申请成为服务中心
	function apply_agent(){

		$username = session('username');

		$user = Db::name('member')->where('username',$username)->find();


		if($user['is_agent']==1)
		{
			$this->redirect('agent/sub_agent');
		}

		$co = Db::name('agent_apply')->where('username',$username)->find();

		if(!empty($co))
		{
			$this->error('你已经提交了申请');
		}

		$data1['uid']=UID;
		$data1['username']=$username;
		$data1['tel']="";
		$data1['create_time']=time();

		//$data1['type']=$data['type'];
		if(Db::name('agent_apply')->insert($data1,false,true)){
			storage_user_action(UID,member('nickname'),config('FRONTEND_USER'),'申请成为服务中心');

			$this->success('申请成功，请等待审核');
		}else{
			$this->error('申请失败');
		}

	}
	//编辑申请资料
	function edit_apply_agent(){
		if(request()->isPost()){
			$data=input('post.');
			$province_id=null;
			$city_id =null;
			$country_id=null;

			if($data['province']=="省份" ||$data['province']==null)
			{
				return ['error'=>'省份必须填写'];
			}

			if($data['city']=="地级市" || $data['city']==null)
			{
				return ['error'=>'地级市必须填写'];
			}
			if($data['town']=="" ||$data['town']==null)
			{
				return ['error'=>'市、县级市、县必须填写'];
			}

			if($data['id6']=="" ||$data['id6']==null)
			{
				return ['error'=>'请输入所在服务中心区域身份证的前6位'];
			}

			$data['status']=0;
			$username = session('username');
			$data1['uid']=UID;
			$data1['username']=$username;		
			$data1['create_time']=time();
			$data1['province']=$data['province'];
			$data1['city']=$data['city'];
			$data1['country']=$data['town'];
			$data1['id6']=$data['id6'];
			$data1['status']=0;		
			if(Db::name('agent_apply')->where('username',$username)->update($data1,false,true)){
				storage_user_action(UID,member('nickname'),config('FRONTEND_USER'),'编辑申请服务中心资料');
				return ['success'=>'修改成功，请等待审核'];
			}else{
				return ['error'=>'修改失败'];
			}
		}
	}
	
	//我的资料
	function my_agent_info(){
		
		if(request()->isPost()){
			$agent=input('post.');
			
			if(Db::name('agent')->update($agent,false,true)){
				storage_user_action(UID,member('nickname'),config('FRONTEND_USER'),'编辑我的资料');
				return ['success'=>'修改成功'];
			}else{
				return ['error'=>'修改失败'];
			}
			
		}
		$this->assign('agent',Db::name('agent')->where(array('uid'=>UID))->find());
		
		$this->assign('top_title','我的资料');
		return $this->fetch();
	}



	/**
	 * 服务中心基本信息
	 * @return mixed
	 */
	function sub_agent(){		
		
		$username = session("username");
		$userinfo = Db::name('member')->where('username',$username)->find();
		$agent = Db::name('agent')->where(array('uid'=>UID))->find();

//		echo'<pre>';print_r($agent);exit;

		$prv = Db::name('area')->where('area_id',$agent['province_id'])->find();
        $city = Db::name('area')->where('area_id',$agent['city_id'])->find();
        $country = Db::name('area')->where('area_id',$agent['country_id'])->find();

        $area = $prv['area_name'].' '.$city['area_name'].' '.$country['area_name'];

        $map['user_state'] = array('gt',0);
        $usernum = Db::name('member')->where(array('id6'=>$agent['id6']))->where($map)->count();
        //var_dump($usernum);
        //die;
        //var_dump($usernum);
        //die;


		$order=Db::name('agent_bonus')
			->alias('ab')				
			->join('member m','ab.buyer_id = m.uid','left')		
			->field('ab.*,m.nickname,m.userpic')	
			->where(array('ab.uid'=>UID))
			->order('ab.ab_id desc')
			->select();
		
	    $state="正在考核";
		if($agent['state']==1)
		{
			$state="考核通过";
		}

		
		if($agent['state']==2)
		{
			$state="考核不通过";
		}

		$agent_area = Db::name('agent_area')->where('username',$username)->select();
		$this->assign('agent_area',$agent_area);

		$this->assign('agent',Db::name('agent')->where(array('uid'=>UID))->find());
		$this->assign('userinfo',$userinfo);
		$this->assign('today',Db::name('agent_bonus')->where([
				'order_status_id'=>config('paid_order_status_id'),
				'create_time'=>date('Y-m-d',time()),
				'uid'=>UID
			])->field('SUM(bonus) as total')->select());

		$this->assign('yesterday',Db::name('agent_bonus')->where([
				'order_status_id'=>config('paid_order_status_id'),
				'create_time'=>date('Y-m-d',strtotime("-1 day")),
				'uid'=>UID
			])->field('SUM(bonus) as total')->select());
		$this->assign('member',Db::name('member')->where(array('pid'=>UID))->count());
		$this->assign('area',$area);
		$this->assign('usernum',$usernum);
		$this->assign('agent',$agent);
		$this->assign('state',$state);
		$this->assign('list',$order);
		$this->assign('empty',"<span style='margin-left:20px;'>没有订单数据</span>");
		$this->assign('top_title','我的服务中心');
		return $this->fetch();
	}



	function ajax_member_list(){
		
		$page=input('param.page');//页码
        //开始数字,数据量
		$limit = (12 * $page) . ",12";
		
		$list=Db::name('member')->where(['pid'=>UID])->field('nickname,userpic,regdate')->order('uid desc')->limit($limit)->select();
		
		foreach ($list as $k => $v) {
			$list[$k]['create_time']=date('Y-m-d',$v['regdate']);
		}
		
		return $list;
	}
}
